import { observable, action, toJS } from 'mobx';
import * as API_MOVE from '../apis/move';
import { ComsStore } from './tables/common';
import { Modal } from 'antd';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class DocMoveStore extends ComsStore {
  @observable title = getLabel(18052, '移动复制');
  @observable loading = false;
  @observable categorys = {
    categorys: [],
  };
  @observable srcCustoms = {
    customs: [],
  };
  @observable objCustoms = {
    customs: [],
  };
  @observable condition = {};
  @observable search = {};
  @observable list = {};
  @observable show = {
    docShow: false,
    moveShow: true,
  }
  @observable ids = {
    srcsecid: '',
    objsecid: '',
  }
  @observable values = {
    srcValue: '',
    objValue: '',
  }
  @observable srcKey = '';
  @observable objKey = '';
  @observable datas = [];
  @observable rowKeys = [];
  @observable listDatas = [];
  @observable tabKey = 'copy';
  @observable showSearchAd = false;
  @observable showThumbnails = false;
  @observable checkedKeys = [];
  @observable srcBrowserConditionParam;
  @observable objBrowserConditionParam;
  @observable showDocLog = false;
  @observable srcItems = [];
  @observable objItems = [];
  @observable buttonHide = false;
  @action
  setTitle = (title) => {
    this.title = title;
  }

  setLoading = (loading) => {
    this.loading = loading;
  }
 setButtonHide = (buttonHide) => {
    this.buttonHide = buttonHide;
  }
  setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
    return this;
  }

  // 路由切换回到界面初始状态
  clearShow = () => {
    this.show = {
      docShow: false,
      moveShow: true,
    };
    this.ids = {
      srcsecid: '',
      objsecid: '',
    };
    this.srcCustoms = {
      customs: [],
    };
    this.objCustoms = {
      customs: [],
    };
    this.datas = [];
    this.listDatas = [];
    this.tabKey = 'copy';
    this.comsWeaTableStore.setSelectedRowKeys([]);
  }

  // 移动、复制tab切换
  changeTabKey = (tabKey) => {
    this.clearShow();
    this.tabKey = tabKey;
    if (this.tabKey === 'move') {
      this.moveCategory({ operation: 0 });
    } else {
      this.moveCategory({ operation: 1 });
    }
    this.comsWeaTableStore.setSelectedRowKeys([]);
  }

  // 复制 、移动并重置共享
  moveOrCopy = (buttonKey) => {
    let srcsecid = this.ids.srcsecid,
objsecid = this.ids.objsecid,
      tabKey = this.tabKey,
selectedRowKeys = toJS(this.comsWeaTableStore.selectedRowKeys).join(',');
    let datas = toJS(this.datas);
    let srcKey,
objKey,
params;
    datas.map((item) => {
      srcKey = item.srcKey;
      objKey = item.objKey;
      this.listDatas.push(`${srcKey}_${objKey}`);
    });
    let listDatas = toJS(this.listDatas).join(',');
    params = {
      sourceId: srcsecid,
      targetId: objsecid,
      docids: selectedRowKeys,
      operate: tabKey,
      refids: listDatas,
    };
    if (buttonKey === 'copy') {
      API_MOVE.moveOrCopy(params).then((data) => {
        if (data.status == 1) {
          Modal.info({
            title: getLabel(15172, '系统提示'),
            content: data.msg,
          });
          this.doBack();
        } else {
          Modal.info({
            title: getLabel(15172, '系统提示'),
            content: getLabel(83912, '操作失败！'),
          });
        }
        this.buttonHide = false;
      });
    } else if (buttonKey === 'reset') {
      API_MOVE.moveOrCopyReset(params).then((data) => {
        Modal.info({
          title: getLabel(15172, '系统提示'),
          content: data.msg,
        });
        if (data.status == 1) {
          this.doBack();
        }
        this.buttonHide = false;
      });
    }
  }

  // 源目录和目标目录改变--目录不同
  srcChange = (ids, names, replaceDatas) => {
    let objsecid = this.ids.objsecid;
    if (ids) {
      if (ids !== objsecid) {
        this.ids = {
          srcsecid: ids,
          objsecid,
        };
        this.customField({ secid: ids, type: '1' });
        this.srcBrowserConditionParam = { ...this.srcBrowserConditionParam, replaceDatas };
      } else {
        Modal.info({
          title: getLabel(15172, '系统提示'),
          content: getLabel(385475, '源目录和目标目录不能相同'),
        });
        this.categorys.categorys = [];
        this.srcCustoms.customs = [];
        this.datas = [];
        this.srcBrowserConditionParam = { ...this.srcBrowserConditionParam, replaceDatas: [] };
      }
      return (ids === objsecid);
    }
      this.ids = { ...this.ids, ...{ srcsecid: '' } };
      this.categorys.categorys = [];
      this.srcCustoms.customs = [];
      this.datas = [];
      this.srcBrowserConditionParam = { ...this.srcBrowserConditionParam, replaceDatas: [] };
  }

  objChange = (ids, names, replaceDatas) => {
    let srcsecid = this.ids.srcsecid;
    if (ids) {
      if (ids !== srcsecid) {
        this.ids = {
          srcsecid,
          objsecid: ids,
        };
        this.customField({ secid: ids, type: '0' });
        this.objBrowserConditionParam = { ...this.objBrowserConditionParam, replaceDatas };
      } else {
        Modal.info({
          title: getLabel(15172, '系统提示'),
          content: getLabel(385475, '源目录和目标目录不能相同'),
        });
        this.categorys.categorys = [];
        this.objCustoms.customs = [];
        this.datas = [];
        this.objBrowserConditionParam = { ...this.objBrowserConditionParam, replaceDatas: [] };
      }
      return (ids === srcsecid);
    }
      this.ids = { ...this.ids, ...{ objsecid: '' } };
      this.categorys.categorys = [];
      this.objCustoms.customs = [];
      this.datas = [];
      this.objBrowserConditionParam = { ...this.objBrowserConditionParam, replaceDatas: [] };
  }

  // radioChange
  srcRadioChange = (val) => {
    let objValue = this.values.objValue;
    this.values = {
      srcValue: val,
      objValue,
    };
  }
  objRadioChange = (val) => {
    let srcValue = this.values.srcValue;
    this.values = {
      srcValue,
      objValue: val,
    };
  }

  // 添加相关
  addPush = () => {
    let srcValue = this.values.srcValue,
objValue = this.values.objValue;
    let srcFieldname,
objFieldname,
scrFieldlabel,
objFieldlabel;
    if (this.srcCustoms.customs.length !== 0) {
      this.srcCustoms.customs[0].items[0].items.map((arr) => {
        if (arr.fieldid == srcValue) {
          srcFieldname = arr.fieldname;
          scrFieldlabel = arr.fieldlabel;
        }
      });
    }

    if (this.objCustoms.customs.length !== 0) {
      this.objCustoms.customs[0].items[0].items.map((arr) => {
        if (arr.fieldid == objValue) {
          objFieldname = arr.fieldname;
          objFieldlabel = arr.fieldlabel;
        }
      });
    }

    if (srcFieldname && objFieldname && srcFieldname === objFieldname) {
      this.datas.push({
        srcKey: srcValue,
        objKey: objValue,
        src: scrFieldlabel,
        srcType: srcFieldname,
        obj: objFieldlabel,
        objType: objFieldname,
      });
      this.values = {
        srcKey: srcValue,
        objKey: objValue,
      };
    } else {
      Modal.info({
        title: getLabel(15172, '系统提示'),
        content: getLabel(385477, '请选择类型一致的自定义字段'),
      });
    }
  }
  // 添加自定义字段
  add = () => {
    let srcValue = this.values.srcValue,
objValue = this.values.objValue,
datas = this.datas;
    if (datas.length === 0) {
      this.addPush();
    } else if (datas.some(key => (key.srcKey === srcValue || key.objKey === objValue))) {
      Modal.info({
        title: getLabel(15172, '系统提示'),
        content: getLabel(385484, '相同的字段不能重复选择'),
      });
    } else {
      this.addPush();
    }
  }

  // 删除相关
  selectedChange = (selectedRowKeys) => {
    this.rowKeys = selectedRowKeys;
  }

  del = () => {
    let rowKeys = toJS(this.rowKeys),
datas = toJS(this.datas);
    this.datas = datas.filter((val, index) => rowKeys.indexOf(index) < 0);
    this.rowKeys = [];
  }

  doBack = () => {
    this.show = {
      docShow: false,
      moveShow: true,
    };
  }

  doShow = () => {
    if (this.ids.srcsecid && this.ids.objsecid) {
      this.comsWeaTableStore.setSelectedRowKeys([]);
      this.show = {
        docShow: true,
        moveShow: false,
      };
      this.doSearch('MoveCopy', { sourceId: this.ids.srcsecid });
    } else {
      Modal.info({
        title: getLabel(15172, '系统提示'),
        content: getLabel(385110, '请选择目录'),
      });
    }
  }

  moveCategory = (params = { operation: '' }) => {
    API_MOVE.moveCategory(params).then((data) => {
      this.categorys = data;
      this.srcBrowserConditionParam = data.categorys[0].items[0].browserConditionParam;
      this.objBrowserConditionParam = data.categorys[0].items[1].browserConditionParam;
    });
  }

  customField = (params = { secid: '', type: '' }) => {
    API_MOVE.moveCustomField(params).then((data) => {
      if (params.type === '1') {
        this.srcCustoms = data;
        this.srcItems = data.customs[0].items[0].items;
      } else if (params.type === '0') {
        this.objCustoms = data;
        this.objItems = data.customs[0].items[0].items;
      }
    });
  }
}

